package com.wk1un2.controller;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wk1un2.entity.Messages;
import com.wk1un2.service.MessagesService;
import com.wk1un2.util.Result;

@RequestMapping("/public")
@RestController
public class MessagesController {
	@Autowired
	private MessagesService messagesservice;

	@RequestMapping("getAll")
	public Object getAll(@RequestParam Map<String, Object> map, Integer page, Integer limit) {
		map.put("page", (page - 1) * limit);
		map.put("limit", limit);
		Result res = new Result(0, "success", messagesservice.getAll(map).size(), messagesservice.getAll(map));
		return res;
	}

	@RequestMapping("add")
	public Object add(Messages msg) {
		msg.setMesstate("未读");
		msg.setMessendtime(new Date());
		Result res = new Result(0, "success", messagesservice.add(msg), true);
		return res;
	}

	@RequestMapping("update")
	public Object update(Messages msg) {
		msg.setMesstate("已读");
		Result res = new Result(0, "success", messagesservice.update(msg), true);
		return res;
	}

	@RequestMapping("delete")
	public Object delete(Integer mesid) {
		Result res = new Result(0, "success", messagesservice.delete(mesid), true);
		return res;
	}
	/**
	 * 未读消息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("/getUnreadMessages")
	public Object getUnreadMessages(@RequestParam Map<String, Object> map) {
		Result res = new Result(0, "success", messagesservice.getUnreadMessages(map).size(), messagesservice.getUnreadMessages(map));
		return res;
	}
	/**
	 * 已读消息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("/getReadMessages")
	public Object getReadMessages(@RequestParam Map<String, Object> map) {
		Result res = new Result(0, "success", messagesservice.getReadMessages(map).size(), messagesservice.getReadMessages(map));
		return res;
	}
	
}
